<?php
/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

class DRX_UrlParser {
	function GetRootPath() {
		return str_replace("/index.php","/",$_SERVER['PHP_SELF']);
	}

	/*
	 * Input query String
	 * Output ModRewrite URL
	 *
	 */
	function UrlToModrewrite($para)
	{
		$arrParameter = explode("&",$para);
		if(preg_match("/m=/i", $arrParameter[0])) {
			$UrlRewrite.= str_replace("m=","/",$arrParameter[0]);
		}
		else {
			$UrlRewrite.= "/".DEFAULT_CONTROLLER;
		}

		if(preg_match("/a=/i", $arrParameter[1])) {
			$UrlRewrite.= str_replace("a=","/",$arrParameter[1]);
		}
		else {
			$UrlRewrite.= "/".DEFAULT_CONTROLLER_ACTION;
		}

		if(preg_match("/args=/i", $arrParameter[2])) {
			$UrlRewrite.= str_replace("args=","/",$arrParameter[2]);
		}
		return $UrlRewrite;
	}

	function GetModRewriteURL($key_filter="")
	{
		$m 		= DRX::load("controller");
		$a 		= DRX::load("action");
		$args 	= DRX::load("args");
		if($key_filter!="") $args = $this->FilterInArgs($key_filter);
		if($args!="") $args=$args."&";		
		$args = "?".$args;

		return $m."/".$a."/".$args;
	}

	function FilterInArgs($key)
	{
		// get back "merged args" (composed by url args and request args)
		$args 	= DRX::load("args");

		$arrExps = array();
		
		if ($args!="")
		{
			$arrExps = array_reverse(explode("&",$args));
			
			for($i=0; $i<sizeof($arrExps); $i++) 
			{
				list($k, $v) = explode("=", $arrExps[$i]);
				if ($k == $key)
				{
					unset($arrExps[$i]);
					break;
				}
			}
		}

		return implode("&",array_reverse($arrExps));
	}
}
